
class BiliRankingImage
  require 'RMagick'
  include Magick
  require 'jcode'
  attr_accessor :path
  alias :pa :path
  alias :pa= :path=
  FONT_EXE = "#{RAILS_ROOT}/public/acfun_list_font_pic.exe"
  TMP_FILE = 'tmp.png'
  @@iconv ||= Iconv.new 'GB18030', 'UTF-8//IGNORE'
  FONT_FZZHY_GBK = @@iconv.iconv "方正准圆_GBK"
  FONT_FZKTJ = @@iconv.iconv "方正卡通简体"
  FONT_HYZHJ = @@iconv.iconv "汉仪中黑简"
  FONT_HYZJTJ = @@iconv.iconv "汉仪竹节体简"
  FONT_KT = @@iconv.iconv "楷体"
  FONT_FZZYJ = @@iconv.iconv "方正综艺简体"
  CHENG = @@iconv.iconv "×"
  FONT_DIGI = "DigifaceWide"
  FONT_WRYH = @@iconv.iconv "微软雅黑"

  def self.new pa
    brl = super()
    brl.path = pa
    brl
  end

  def make_ranking_img1 weekly_result
    d = weekly_result
    work = Work.find_by_wid d.wid
    @draw = Draw.new
   case Result.get_date_revised(work.cdate)
   when 1.0
     img = ImageList.new(public_pa + 'model_sp_4.png')
   when 1.2
     img = ImageList.new(public_pa + 'model_sp_3.png')
   when 1.4
     img = ImageList.new(public_pa + 'model_sp_2.png')
   when 1.5
     img = ImageList.new(public_pa + 'model_sp_1.png')
   end
    d_name = @@iconv.iconv(d.name)
    case d_name.size
      when 0..44 then j_title_size = 18
      when 45..50 then  j_title_size = 17
      when 51..55 then j_title_size = 16
      when 56..60 then  j_title_size = 15
      when 61..65 then j_title_size = 14
      else
        j_title_size = 13
     end
    #标题
    draw_font d_name, FONT_FZZHY_GBK, j_title_size, "FF111111", 15, 327
    #ID,PAD,DATE
    @draw.gravity NorthGravity
    draw_font "av#{d.wid}", FONT_FZZYJ, 23, "FF111111", 199, 352
    draw_font @@iconv.iconv(d.wtype), FONT_FZZHY_GBK, 20, "FF111111", 68, 354
    draw_font work.cdate.strftime("%Y-%m-%d %H:%M"), FONT_FZZYJ, 22, "FF111111", -126, 352
    #总分
    @draw.gravity NorthEastGravity
    draw_font Result.point_str(d.score), FONT_DIGI, 22, "FFCC0000", 28, 295
    #单名次
    draw_font d.clicks_rank, FONT_FZZHY_GBK, 18, "FFFFFFFF", 22, 72
    draw_font d.comments_rank, FONT_FZZHY_GBK, 18, "FFFFFFFF", 22, 173
    draw_font d.stows_rank, FONT_FZZHY_GBK, 18, "FFFFFFFF", 22, 224
    #点击分
    revised_clicks = Result.get_click_revised(d.part)
    fixed_clicks = d.clicks * revised_clicks
    draw_font Result.point_str(d.clicks), FONT_DIGI, 22, "FFFF0000", 1, 90
    #评论分
    revised_comments = Result.get_comment_revised(fixed_clicks, d.comments)
    draw_font Result.point_str(d.comments), FONT_DIGI, 22, "FFFF0000", 36, 195
    #收藏分
    draw_font Result.point_str(d.stows), FONT_DIGI, 22, "FFFF0000", 36, 245
    #名次
    @draw.gravity NorthGravity
    if d.score_rank.to_s.size == 3
      draw_font d.score_rank, FONT_DIGI, 48, "FFCC0000", 197, 3
    elsif d.score_rank.to_s.size == 4
      draw_font d.score_rank, FONT_DIGI, 37, "FFCC0000", 195, 10
    elsif d.score_rank.to_s.size == 5
      draw_font d.score_rank, FONT_DIGI, 28, "FFCC0000", 196, 16
    else
      draw_font d.score_rank, FONT_DIGI, 55, "FFCC0000", 199, -3
    end
    draw_font d.part, FONT_WRYH, 25, "FFFF0000", 202, 140
    #倍率
    draw_font "#{CHENG}#{revised_clicks}", FONT_FZZHY_GBK, 16, "FF0B4776", 228, 123
    draw_font "#{CHENG}#{revised_comments}", FONT_FZZHY_GBK, 18, "FF0B4776", 231, 201
    draw_font "#{CHENG}#{50}", FONT_FZZHY_GBK, 18, "FF0B4776", 231, 251
    @draw.draw img
    img_path = pa + "2_list1\\top#{d.score_rank}.png"
    img.write img_path
  end

  def make_ranking_img2 weekly_results
    img = nil
    img_index = 96
    weekly_results.each_with_index do |d, i|
        e_i = i % 3
        work = Work.find_by_wid d.wid
        d_name = @@iconv.iconv(d.name).gsub(/\"/,'\\"')
        img = ImageList.new(pa + '3_list2\\_model.png') if e_i.zero?
        @draw = Draw.new
        case d_name.size
          when 0..38 then j_title_size = 22
          when 39..41 then j_title_size = 21
          when 42..44 then j_title_size = 20
          when 45..47 then j_title_size = 19
          when 48..50 then j_title_size = 18
          when 51..53 then  j_title_size = 17
          when 54..56 then  j_title_size = 16
          when 57..59 then j_title_size = 15
          when 60..62 then j_title_size = 14
          else j_title_size = 13
        end
        #封面
        pic_path = work.pic_path.to_i.zero? ? %[http://#{BILI_PATH}/#{work.pic_path}] : %[http://#{BILI_PATH}/uploads/userup/#{work.pic_path}]
        pic_data =  open(pic_path){|f| f.read} rescue nil
        if pic_data
          File.open('fengmian', 'wb'){|f| f.write(pic_data)}
          @draw.composite 7, 34+120*e_i, 120, 90, ImageList.new('fengmian')
        else
          @draw.composite 7, 34+120*e_i, 120, 90, ImageList.new('nofengmian.png')
        end
        #标题
        draw_font d_name, FONT_FZZHY_GBK, j_title_size, "FF222222", 18, 8+120*e_i
        #名次
        @draw.gravity NorthGravity
        p d.score_rank
        draw_font d.score_rank, FONT_FZKTJ, 70, "FFFF3300", -81, 22+120*e_i
        #ID,PAD,DATE
        draw_font "av#{d.wid}", FONT_HYZHJ, 18, "FFFFFFFF", -82, 107+120*e_i
        draw_font d.part, FONT_HYZHJ, 22, "FFCC0000", 235, 46+120*e_i
        draw_font work.cdate.strftime("%Y-%m-%d %H:%M"), FONT_FZZHY_GBK, 17, "FFFFFFFF", 135, 107+120*e_i
        #总分
        @draw.gravity NorthEastGravity
        score_color = "FFFF3300"
        draw_font Result.point_str(d.score), FONT_HYZHJ, 25, score_color, 48, 77+120*e_i
        #单分
        draw_font Result.point_str(d.clicks), FONT_HYZHJ, 18, "FFCC0000", 175, 38+120*e_i
        draw_font Result.point_str(d.comments), FONT_HYZHJ, 18, "FFCC0000", 193, 60+120*e_i
        draw_font Result.point_str(d.stows), FONT_HYZHJ, 18, "FFCC0000", 202, 83+120*e_i
        #单名次
        @draw.gravity NorthGravity
        draw_font d.clicks_rank, FONT_HYZHJ, 18, "FF663366", 130, 38+120*e_i
        draw_font d.comments_rank, FONT_HYZHJ, 18, "FF663366", 130, 60+120*e_i
        draw_font d.stows_rank, FONT_HYZHJ, 18, "FF663366", 74, 83+120*e_i
        #修正
        @draw.gravity NorthWestGravity
        draw_font "#{CHENG}#{Result.get_click_revised d.part}", FONT_FZZHY_GBK, 15, "FF0033FF", 327, 38+120*e_i
        draw_font "#{CHENG}#{Result.get_comment_revised(d.clicks*Result.get_click_revised(d.part), d.comments)}", FONT_FZZHY_GBK, 15, "FF0033FF", 310, 60+120*e_i

        @draw.draw img
        img.write %[#{pa}3_list2\\#{'0' * (3-(img_index += 1).to_s.size)}#{img_index}.png] if e_i == 2
     end
  end

  private
  def draw_font str, font, font_size, color, x, y
    system %[#{FONT_EXE} "#{str}" #{color} #{font_size} #{font}"]
    @draw.composite x, y, 0, 0, ImageList.new(TMP_FILE)
  end
  
  def public_pa;  %[#{pa}public\\]; end
  def list1_pa;  %[#{pa}2_list1\\]; end
  def list2_pa;  %[#{pa}3_list2\\]; end
end
